<template>
  <div class="login_container">
    <div class="login_box">
      <div class="login_head">
        <img src="../assets/cangjian.jpg" />
      </div>
      <div class="login_body">
        <!-- ref：拿到该组件的实例对象 -->
        <el-form
          ref="loginFormRef"
          class="login_form"
          :model="logform"
          :rules="logrules"
        >
          <!-- 用户名 -->
          <el-form-item prop="username">
            <el-input
              prefix-icon="iconfont icon-users"
              v-model="logform.username"
            ></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input
              type="password"
              prefix-icon="iconfont icon-3702mima"
              v-model="logform.password"
            ></el-input>
          </el-form-item>
          <el-form-item class="login_button">
            <el-button type="primary" @click="login">登陆</el-button>
            <el-button type="info" @click="logreset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      // 表单的数据
      logform: {
        username: 'admin',
        password: '123456'
      },
      //   表单的验证规则
      logrules: {
        //  验证用户名是否合法
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, message: '用户名长度必须大于3', trigger: 'change' }
        ],
        //  验证密码是否合法
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 3,
            max: 16,
            message: '密码长度在3-16位之间！',
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    // 点击重置按钮，重置表单
    logreset () {
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) {
          alert('用户名密码格式有误')
        } else {
          // 对象的解构赋值的内部机制，是先找到同名属性，然后再赋给对应的变量，如果没找到则为undefined
          const { data: res } = await this.$http.post('login', this.logform)
          if (res.meta.status === 200) {
            // element-ui中的弹框组件
            console.log(res)
            this.$message.success(res.meta.msg)
            window.sessionStorage.setItem('token', res.data.token)
            this.$router.push('/home')
            // 登陆成功后的操作行为
            //  1.将登陆成功之后的token保存到sessionStorage中
            //  2.token只应当在打开网站期间生效，所以保存在sessionStorage中
            //  3.通过编程式导航跳转到后台主页，路由地址/home
          } else {
            this.$message.error(res.meta.msg)
          }
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  width: 100%;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.login_box > .login_head {
  width: 130px;
  height: 130px;
  overflow: hidden;
  border-radius: 50%;
  position: absolute;
  left: 50%;
  top: -50%;
  transform: translate(-50%, 60%);
}

.login_box > .login_head > img {
  width: 100%;
  height: 100%;
}

.login_form {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.login_button {
  display: flex;
  justify-content: flex-end;
}
</style>
